#!/usr/bin/env bash

# Set environment variables for dev
# Change settings for CI
if [ ! -z "$BUILD_NUMBER" ]; then
    # We need the full path here because /sbin is not in user Jenkin's $PATH
    export XDEBUG_HOST=$(/sbin/ifconfig docker0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1)
else
    export XDEBUG_HOST=$(ipconfig getifaddr en1)
fi

export CONTAINER_ENV=${CONTAINER_ENV:-local}
export APP_PORT=${APP_PORT:-80}
export DB_PORT=${DB_PORT:-3306}
export DB_ROOT_PASS=${DB_ROOT_PASS:-secret}
export DB_NAME=${DB_NAME:-homestead}
export DB_USER=${DB_USER:-homestead}
export DB_PASS=${DB_PASS:-secret}

# Decide which docker-compose file to use
COMPOSE_FILE="dev"

# Disable pseudo-TTY allocation for CI (Jenkins)
TTY=""

# Change settings for CI
if [ ! -z "$BUILD_NUMBER" ]; then
    COMPOSE_FILE="ci"
    TTY="-T"
fi

# Create docker-compose command to run
COMPOSE="docker-compose -f docker-compose.${COMPOSE_FILE}.yml"

# If we pass any arguments...
if [ $# -gt 0 ];then

    # If "art" is used, pass-thru to "artisan"
    # inside a new container
    if [ "$1" == "art" ]; then
        shift 1
        $COMPOSE run --rm $TTY\
            -w /var/www/html \
            app \
            php artisan "$@"

    # If "composer" is used, pass-thru to "composer"
    # inside a new container
    elif [ "$1" == "composer" ]; then
        shift 1
        $COMPOSE run --rm $TTY\
            -w /var/www/html \
            app \
            composer "$@"

    # If "test" is used, run unit tests,
    # pass-thru any extra arguments to php-unit
    elif [ "$1" == "test" ]; then
        shift 1
        $COMPOSE run --rm $TTY\
            -w /var/www/html \
            app \
            ./vendor/bin/phpunit "$@"
            
    elif [ "$1" == "t" ]; then
        shift 1
        $COMPOSE exec \
            app \
            sh -c "cd /var/www/html && ./vendor/bin/phpunit $@"

    # If "npm" is used, run npm
    # from our node container
    elif [ "$1" == "npm" ]; then
        shift 1
        $COMPOSE run --rm $TTY\
            -w /var/www/html \
            node \
            npm "$@"

    # If "gulp" is used, run gulp
    # from our node container
    elif [ "$1" == "gulp" ]; then
        shift 1
        $COMPOSE run --rm $TTY\
            -w /var/www/html \
            node \
            ./node_modules/.bin/gulp "$@"
    else
        $COMPOSE "$@"
    fi

else
    $COMPOSE ps
fi